<?php

namespace App\Remote\XiaoHongShu;

use App\Exceptions\BaseException;
use Closure;
use GuzzleHttp\Exception\GuzzleException;
use Symfony\Component\HttpFoundation\Request as RequestAlias;
use Symfony\Component\HttpFoundation\Response as ResponseAlias;

class XhsCouponRemote extends XhsBusinessBaseRemote
{

    /**
     * 小红书返回数据预处理
     *
     * @return Closure
     */
    public static function registerResponseHandle(): Closure
    {
        return function () {
            list($responseHttpCode, $responseContent) = func_get_args();
            if ($responseHttpCode != ResponseAlias::HTTP_OK) {
                throw new \Exception("{$responseHttpCode} . [{$responseContent}]");
            }

            if (isJson($responseContent)) {
                return jsonToArr($responseContent);
            }

            throw new \Exception("[ $responseHttpCode ]-[ $responseContent ]");
        }; // TODO: Change the autogenerated stub
    }

    /**
     * 核销券码
     *
     * @param string $couponCode
     * @return mixed
     * @throws BaseException
     * @throws GuzzleException
     */
    public static function verifyCoupon(string $couponCode)
    {
        return self::customRequestV3(
            "api/locallife/verifyCoupon",
            RequestAlias::METHOD_POST,
            ["coupon_code" => $couponCode]
        );
    }

    /**
     * 券码取消核销
     *
     * @param string $couponCode
     * @param string $verifyId
     * @return mixed
     * @throws BaseException
     * @throws GuzzleException
     */
    public static function verifyCancelCoupon(string $couponCode, string $verifyId)
    {
        return self::customRequestV3(
            "api/locallife/verifyCancel",
            RequestAlias::METHOD_POST,
            [
                "coupon_code" => $couponCode,
                "verify_id"   => $verifyId
            ]
        );
    }

    /**
     * 查询券码信息
     *
     * @param string $couponCode
     * @return mixed
     * @throws BaseException
     * @throws GuzzleException
     */
    public static function couponInfo(string $couponCode)
    {
        return self::customRequestV3(
            "api/locallife/couponInfo",
            RequestAlias::METHOD_POST,
            ["coupon_code" => $couponCode]
        );
    }


}
